package com.spring.framework.demo.action;


import com.spring.framework.demo.service.IModifyService;
import com.spring.framework.demo.service.IQueryService;
import com.spring.framework.annotation.LBAutowired;
import com.spring.framework.annotation.LBController;
import com.spring.framework.annotation.LBRequestMapping;
import com.spring.framework.annotation.LBRequestParam;
import com.spring.framework.webmvc.LBModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 公布接口url
 * @author Tom
 *
 */
@LBController
@LBRequestMapping("/web")
public class MyAction {

	@LBAutowired
    IQueryService queryService;
	@LBAutowired
    IModifyService modifyService;

	@LBRequestMapping("/query.json")
	public LBModelAndView query(HttpServletRequest request, HttpServletResponse response,
                                @LBRequestParam("name") String name){
		String result = queryService.query(name);
		System.out.println(result);
		return out(response,result);
	}

	@LBRequestMapping("/add*.json")
	public LBModelAndView add(HttpServletRequest request, HttpServletResponse response,
                              @LBRequestParam("name") String name, @LBRequestParam("addr") String addr){
		String result = modifyService.add(name,addr);
		return out(response,result);
	}

	@LBRequestMapping("/remove.json")
	public LBModelAndView remove(HttpServletRequest request, HttpServletResponse response,
                                 @LBRequestParam("id") Integer id){
		String result = modifyService.remove(id);
		return out(response,result);
	}

	@LBRequestMapping("/edit.json")
	public LBModelAndView edit(HttpServletRequest request, HttpServletResponse response,
                               @LBRequestParam("id") Integer id,
                               @LBRequestParam("name") String name){
		String result = modifyService.edit(id,name);
		return out(response,result);
	}



	private LBModelAndView out(HttpServletResponse resp, String str){
		try {
			resp.getWriter().write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
